﻿using K9Nano.Logging;
using Microsoft.EntityFrameworkCore;

namespace K9Nano.Domain.Logging;

public class LogDbContext : DbContext
{
    public LogDbContext(DbContextOptions<LogDbContext> options) : base(options)
    {
    }

    public DbSet<ApiLogEntity> Logs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ApiLogEntity>(logs =>
        {
            logs.ToTable("httplog");

            logs.HasKey(a => a.Id);
            logs.Property(a => a.Id).ValueGeneratedOnAdd();

            logs.HasIndex(a => a.CreatedAt)
                .HasDatabaseName("idx_time");

            logs.HasIndex(a => a.UserId)
                .HasDatabaseName("idx_user");

            logs.HasIndex(a => new { a.ControllerName, a.ActionName })
                .HasDatabaseName("idx_action");
        });
    }

    public virtual int BulkInsert(IEnumerable<ApiLogEntity> entities)
    {
        Logs.AddRange(entities);
        return SaveChanges();
    }
}
